﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Meqs.Entity;
using Meqs.Logic.Interface;
using System.Collections.Generic;

public partial class School_Manage_InputScore_InputExam : System.Web.UI.Page
{
//    protected int currExamId = 0, currClassType = -1,currGrade = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
       
    }
 

    /// <summary>
    /// 获取所有局级科目
    /// </summary>
    /// <returns></returns>
    private List<ExamsSchool> GetExams()
    {
        List<ExamsSchool> examsSchool = new List<ExamsSchool>();
        int examId = 0;
        string s = txtExamId.Text;
        if (!string.IsNullOrEmpty(s))
        {
            if (int.TryParse(s, out examId))
            {
                examId = Convert.ToInt32(s);
             //   currExamId = examId;
            }

        }

        Hashtable table = new Hashtable();
        if (examId > 0)
        {
            IExamSubjecyEducationLogic logic = Container<IExamSubjecyEducationLogic>.Instance;
            table.Add("ExamID",examId);
            List<ExamSubjecyEducation> listEduSubject = (List<ExamSubjecyEducation>)logic.FindAll(table);

            int classType = -1, grade = 0;

            s = txtClassType.Text;
            if (!string.IsNullOrEmpty(s))
            {
                if (int.TryParse(s, out classType))
                {
                    classType = Convert.ToInt32(s);
                 //   currClassType = classType;
                }
            }

            s = txtGrade.Text;
            if (!string.IsNullOrEmpty(s))
            {
                if (int.TryParse(s, out grade))
                {
                    grade = Convert.ToInt32(s);
                 //   currGrade = grade;
                }
            }

            if (grade > 0 && classType != -1)
            {
                IExamsSchoolLogic schoolLogic = Container<IExamsSchoolLogic>.Instance;
                table = new Hashtable();
                table.Add("Grade", grade);
                table.Add("ClassType", classType);
                table.Add("SchoolMark", CommonScore.GetSchoolMark);
                
                List<ExamsSchool> listExamSchool = (List<ExamsSchool>)schoolLogic.FindAll(table);

                if (listExamSchool.Count > 0)
                {
                    foreach (ExamsSchool item in listExamSchool)
                    {
                        table = new Hashtable();
                        table.Add("ExamID", item.RID);
                        table.Add("SchoolMark", CommonScore.GetSchoolMark);
                        IExamSubjecySchoolLogic schoolSubjectLogic = Container<IExamSubjecySchoolLogic>.Instance;
                        List<ExamSubjecySchool> listSchoolSubject = (List<ExamSubjecySchool>)schoolSubjectLogic.FindAll(table);
                        if (listSchoolSubject.Count != listEduSubject.Count)
                            continue;
                        else
                        {
                            int index = 0;
                            foreach (ExamSubjecySchool item3 in listSchoolSubject)
                            {
                                int tempSubjectId = item3.SubjectID;
                                foreach (ExamSubjecyEducation item2 in listEduSubject)
                                {
                                    if (tempSubjectId == item2.SubjectID)
                                    {
                                        index++;
                                        break;
                                    }
                                }

                            }
                            if(index == listEduSubject.Count)
                                examsSchool.Add(item);
                            
                        }
                    }
                }
            }
        }
        ExamsSchool newItem = new ExamsSchool();
        newItem.RID = -1;
        newItem.Name = "选择考试";
        examsSchool.Insert(0, newItem);
        return examsSchool;
       
    }
    protected void btnBind_Click(object sender, EventArgs e)
    {
        examName.Text = txtExamName.Text;
        Repeater1.DataSource = GetExams();
        Repeater1.DataBind();
    }


    /// <summary>
    /// 从其他考试导入
    /// </summary>
    /// <param name="schoolExamId"></param>
    /// <returns></returns>
    [System.Web.Services.WebMethod]
    public static int Import(int eduExamId,int schoolExamId,int grade,int classType)
    {
        int n = 0;
        if (schoolExamId > 0 && classType > -1 && grade > 0)
        {
            n = Container<Meqs.Logic.Interface.IGradeScoreEducationLogic>.Instance.ImportScoreFromSchool(eduExamId, CommonScore.GetSchoolMark, schoolExamId, CommonScore.GetGradeStoreTable(grade), classType);
        }
        return n;
    }
}
